package com.scalar.forecast.salesForecastEntry.actionCommands;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.db.DBXException;
import com.scalar.chiptrack.users.User;
import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.salesForecastEntry.AutoGrowingLinkedList;
import com.scalar.forecast.salesForecastEntry.ForecastData;
import com.scalar.forecast.salesForecastEntry.dao.SalesForecastEntryDAO;
import com.scalar.forecast.salesForecastEntry.forms.SalesForecastEntryForm;

/**
 * User: Sujan Kumar
 * Date: July 14, 2005
 * Time: 05:11:56 PM
 */
public class SalesForecastEntrySaveCommand extends ActionCommand
{
    public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response )
    {
        SalesForecastEntryForm forecastEntryForm = ( SalesForecastEntryForm )form;

        boolean success = false;
        DBTransaction dbTransaction = null;
        HashMap errors = new HashMap ();
        HashMap info = new HashMap ();
        HttpSession session = request.getSession();
        User pUser = ( User )session.getAttribute( SessionKeys.USER_KEY);
        try
        {
            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();

            String userId = pUser.getUserId();
            SalesForecastEntryDAO.saveSalesForecastEntry (dbTransaction, forecastEntryForm.getRowDataList(), userId );
            AutoGrowingLinkedList forecastDataList  = forecastEntryForm.getForecastDataList();
            int forecastDataListSize    = ( forecastDataList == null ) ? 0 : forecastDataList.size();
            ForecastData forecastData   = null;
            for ( int i = 0; i < forecastDataListSize; i++ )
            {
                forecastData    = ( ForecastData ) forecastDataList.get ( i );
                if ( forecastData != null )
                {
                    SalesForecastEntryDAO.saveSalesForecastEntry (dbTransaction, forecastData.getRowDataList(), userId );
                }
            }
            success = true;
            info.put( INFO_SALES_FORECAST_SAVE_SUCCESSFULL, null );
            request.setAttribute( SessionKeys.INFO_MESSAGES, info );
        }
        catch ( Exception e )
        {
            errors.put ( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_SAVE_FORECAST );
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
            System.out.println ( "--Exception while saving forecast entry--" );
            e.printStackTrace();
        }
        finally
        {
            if ( dbTransaction != null )
            {
                try
                {
                    if ( success )
                    {
                        dbTransaction.commit();
                    }
                    else
                    {
                        dbTransaction.rollback();
                    }
                }
                catch ( DBXException e )
                {
                    System.out.println ( "--DBX Exception while committing/rollbacking..." );
                }
            }
        }

        session.removeAttribute ( mapping.getName() );
        ActionForward actionForward = null;
        actionForward = mapping.findForward ( "salesForecastEnryNew" );

        return actionForward;
    }
}
